﻿
//ztree 扩展
/**
 *  ztree 多选
 * @param {any} url  树请求地址
 * @param {any} name 元素id
 * @param {any} id   元素隐藏域
 * @param {any} orgTarget  树元素DOM id
 * @param {any} vueObservingObj  vue监听对象
 */
function ParentTreeMultiple(url, name, id, orgTarget, vueObservingObj) {
    var zTreeObj;
    var options = {
        text: 'text',
        key: 'id',
        parentKey: 'parentId',
        nameDOM: name,   //显示的文本框ID，如："#catetoryName"
        idDOM: id   //隐藏的文本框，如："#categoryId"
    }
    var setting = {
        view: { selectedMulti: true },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: { "Y": "", "N": "" } //去掉勾选时级联
        },
        data: {
            key: {
                name: options.text,
                title: options.text
            },
            simpleData: {
                enable: true,
                idKey: options.key,
                pIdKey: options.parentKey,
                rootPId: 'null'
            }
        },
        callback: {
            onClick: onClick,
            onCheck: onCheck
        }
    };
    var showMenu = function () {
        $("#menuContent").css({ left: "10px", top: $("#" + options.nameDOM).outerHeight() + "px" }).slideDown("fast");
        $("body").bind("mousedown", onBodyDown);
    };

    var setCheck = function () {   //设置初始选中的值
        zTreeObj.checkAllNodes(false);

        var observingObj = vueModel.$data;
        var iddom = options.idDOM;

        //此处使用传过来的id 获取不到监听对象
        //暂时组织架构id 写死为OrganizationIds
        //var value = observingObj.entityObj.iddom;
        var value = observingObj.entityObj.OrganizationIds;
        if (value == undefined) return;
        var nodeids = value.split(",");
        $.each(nodeids,
            function () {
                var node = zTreeObj.getNodeByParam("id", this, null);
                if (node != null) {
                    zTreeObj.checkNode(node, true, false);
                }
            });

    }
    function onClick(e, treeId, treeNode) {
        var nodes = zTreeObj.getSelectedNodes();

        for (var i = 0, l = nodes.length; i < l; i++) {
            vueModel.$set(vueObservingObj, options.nameDOM, nodes[i].text);
            vueModel.$set(vueObservingObj, options.idDOM, nodes[i].id);
            break;
        }
        hideMenu();
    }
    function onCheck(e, treeId, treeNode) {
        var nodes = zTreeObj.getCheckedNodes(true);

        var ids = nodes.map(function (e) { return e.id; }).join(",");
        var names = nodes.map(function (e) { return e.text; }).join(",");

        vueModel.$set(vueObservingObj, options.nameDOM, names);
        vueModel.$set(vueObservingObj, options.idDOM, ids);
    }

    function onBodyDown(event) {
        if (!(event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }
    function hideMenu() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }
    return {
        reload: function () {
            var index = layer.load();
            $.getJSON(url,
                {
                    page: 1, rows: 10000
                },
                function (json) {
                    layer.close(index);
                    if (json.length == 0) {
                        vueModel.$set(vueObservingObj, options.nameDOM, '');
                        vueModel.$set(vueObservingObj, options.idDOM, '');
                        return;
                    }
                    zTreeObj = $.fn.zTree.init($("#" + orgTarget), setting, json);
                    setCheck();
                    zTreeObj.expandAll(true);
                    showMenu();
                });
        }
    }
}

/**
 * ztree 单选
 * @param {any} url
 * @param {any} name
 * @param {any} id
 * @param {any} orgTarget
 * @param {any} vueObservingObj
 */
function ParentTree(url, name, id, orgTarget, vueObservingObj) {
    var zTreeObj;
    var options = {
        text: 'text',
        key: 'id',
        parentKey: 'parentId',
        nameDOM: name,   //显示的文本框ID，如："#catetoryName"
        idDOM: id   //隐藏的文本框，如："#categoryId"
    }
    var setting = {
        view: { selectedMulti: false },
        data: {
            key: {
                name: options.text,
                title: options.text
            },
            simpleData: {
                enable: true,
                idKey: options.key,
                pIdKey: options.parentKey,
                rootPId: 'null'
            }
        },
        callback: {
            onClick: onClick
        }
    };
    var showMenu = function () {
        $("#menuContent").css({ left: "10px", top: $("#" + options.nameDOM).outerHeight() + "px" }).slideDown("fast");
        $("body").bind("mousedown", onBodyDown);
    };

    var setCheck = function () {   //设置初始选中的值
        var observingObj = vueModel.$data;
        var value = observingObj.entityObj.ParentId;//此处写死为ParentId
        
        var node = zTreeObj.getNodeByParam("id", value, null);
        if (node != null) {
            zTreeObj.selectNode(node, false, false);
        }
    }
    function onClick(e, treeId, treeNode) {
        var nodes = zTreeObj.getSelectedNodes();

        for (var i = 0, l = nodes.length; i < l; i++) {
            
            vueModel.$set(vueObservingObj, options.nameDOM, nodes[i].text);
            vueModel.$set(vueObservingObj, options.idDOM, nodes[i].id);
            break;
        }
        hideMenu();
    }
    function onBodyDown(event) {
        if (!(event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }
    function hideMenu() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }
    return {
        reload: function () {
            var index = layer.load();
            $.getJSON(url,
                {
                    page: 1, rows: 10000
                },
                function (json) {
                    layer.close(index);
                    if (json.length == 0) {
                        vueModel.$set(vueObservingObj, options.nameDOM, '');
                        vueModel.$set(vueObservingObj, options.idDOM, '');
                        return;
                    }
                    
                    zTreeObj = $.fn.zTree.init($("#" + orgTarget), setting, json);
                    setCheck();
                    zTreeObj.expandAll(true);
                    showMenu();
                });
        }
    }
}